<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2023/12/31
 * Time: 12:29
 */

namespace app\common\service;

use sookie\auth\models\Adminuser;
use sookie\statistical\models\SysAccessLog;
use sookie\auth\models\SysRbacPermissions;
use yii\api\CJsonResponse;
use yii\base\Component;
use yii\base\Exception;
use yii\helpers\ArrayHelper;
use yii\helpers\Url;
use yii\web\ForbiddenHttpException;
use yii\web\Response;

class AuthHandler extends \sookie\bootstraps\handlers\AuthHandler
{
    public function execute(){

        $response = \Yii::$app->response;

        $response->format = Response::FORMAT_JSON;

        //访问路由
        $route = $this->getRoute();

        $result = AuthChecker::doCheck($route,true,true);

        if ($result["ok"] == false){

            $response->data = $result;

            $response->send();exit();
        }

        return true;
    }

    private function getRoute(){

        $route = "/" . \Yii::$app->request->get("r");

        if (strpos($route,"/react/simple/") !== false || strpos($route,"/react/number/") !== false){
            $requestData = ArrayHelper::merge(\Yii::$app->request->get(),\Yii::$app->request->post());
            $page = ArrayHelper::getValue($requestData,"page");
            $route = $route . "/" .  $page;
        }

        //将template 模板下载，替换为import
        $route = str_replace("template","import",$route);

        return trim($route);
    }
}